package javaStu.producerAndConsumer;

import java.util.concurrent.locks.LockSupport;

/**
 * 生产消费模式
 * 用两个线程，一个输出字母，一个输出数字，交替输出1A2B3C4D5E...26Z
 * @author: leiwenkang
 * @date: 2020/7/13
 * @time: 20:22
 */
public class ProducerAndConsumer_LockSupport {
    static Thread t1 = null;
    static Thread t2 = null;

    public static void main(String[] args) {
        char[] ch1 = "1234567".toCharArray();
        char[] ch2 = "ABCDEFG".toCharArray();

        t1 = new Thread(() -> {
            for (char c : ch1) {
                System.out.print(c);
                LockSupport.unpark(t2);
                LockSupport.park();
            }
        }, "t1");


        t2 = new Thread(() -> {
            for (char c : ch2) {
                LockSupport.park();
                System.out.print(c);
                LockSupport.unpark(t1);
            }
        }, "t2");

        t1.start();
        t2.start();
    }

}
